﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using Microsoft.Win32;

namespace _1cun
{
    public partial class Form1 : Form
    {
        PrintDocument pd = new PrintDocument();
        PageSettings pset = new PageSettings();
        PrinterResolution pr = new PrinterResolution();
        string i_text;

        public Form1()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = false;//关闭页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位 
             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "一寸照打印工具" +"版本V"+this.ProductVersion+"运营管理中心制作仅供内部使用";
            string DotNetVersion = "3.5";
           // pset.PaperSize = new PaperSize("A4", 142, 300);百分之一英寸为1单位
            pr.X = 300;
            pr.Y = 300;
            pset.PrinterResolution = pr;
            pset.Landscape = false;//false纵向
            pset.Margins =new Margins(1,2,3,4); // 1单位为百分之一英寸 默认情况下各边的边距都为 1 英寸 
               
            printDocument1.DefaultPageSettings = pset;
            printPreviewControl1.Document = printDocument1;
            // this.printPreviewDialog1.Document.DefaultPageSettings.Landscape = true;//关键这句
            //if (!GetDotNetVersion("4.5"))
            //{
            //    if (MessageBox.Show("当前缺少运行环境，是否进行安装！\r\n\r\n安装完成后将自动启动软件", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
            //        return;
            //    Process.Start(@"_framework4.5.2.exe").WaitForExit(); //一直等待，直到Framework安装完成
            //    if (GetDotNetVersion("4.5"))
            //        Process.Start(@"你的程序.exe");
            //}
            //else
            //    Process.Start(@"你的程序.exe");

            
            if (!GetDotNetVersionbool(DotNetVersion)) {
                MessageBox.Show("您的计算机的.net framework不符合最低要求，程序将退出"  + " 要求" + DotNetVersion + " 本机最大" + GetDotNetVersion_big());
                this.Close();
            }

            i_text = "本机.net版本：" + GetDotNetVersionbool(DotNetVersion).ToString() + " 要求" + DotNetVersion + " 本机最大" + GetDotNetVersion_big();
            Console.WriteLine(i_text);
            Console.WriteLine( printPreviewControl1.Document.PrinterSettings.PrinterResolutions.ToString());
            //Console.WriteLine("打印机支持的分辨率" + pset.PrinterSettings.PrinterResolutions.ToString());
            foreach (PaperSize psize in this.printDialog1.Document.PrinterSettings.PaperSizes )
            {
                Console.WriteLine("纸张大小" + psize.PaperName +"高" +psize.Height+ "宽"+ psize.Width);
                if (psize.PaperName == "A6") {
                    //MessageBox.Show("检测到默认打印机纸张大小有A6可选，","纸张检测");
                    string message = "检测到默认打印机纸张大小有A6可选? 要设置成A6吗？";
                    string caption = "纸张检测";
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;                  
                    result = MessageBox.Show(this, message, caption, buttons,
                        MessageBoxIcon.Question, MessageBoxDefaultButton.Button1 );
                    if (result == DialogResult.Yes)
                    {
                        printDocument1.DefaultPageSettings.PaperSize = psize;
                        printPreviewControl1.InvalidatePreview();//立即刷新预览
                        break;
                    }
                    else {

                    }

                }

                                //                纸张大小Letter高1100宽850
                                //  纸张大小Legal高1400宽850
                                //纸张大小Executive高1050宽725
                                //纸张大小Oficio 8.5 x 13高1300宽850
                                //纸张大小4x6高600宽400
                                //纸张大小5x8高800宽500
                                //纸张大小A4高1169宽827
                                //纸张大小A5高827宽583
                                //纸张大小A6高583宽413


            }//纸张检测

        }
        /// <summary>
        /// 判断.Net Framework的Version是否符合需要
        /// (.Net Framework 版本在2.0及以上)
        /// </summary>
        /// <param name="version">需要的版本 version = 4.5</param>
        /// <returns></returns>
        private static bool GetDotNetVersionbool(string version)
        {
            int i = 0;
            string oldname = "0";
            using (RegistryKey ndpKey =
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                { Console.WriteLine("循环次数"+ ++i);
                    if (versionKeyName.StartsWith("v"))
                    {
                        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                        string newname = (string)versionKey.GetValue("Version", "");
                        if (string.Compare(newname, oldname) > 0)
                        {
                            oldname = newname;

                        }
                        Console.WriteLine("外循环oldname " + oldname);
                        Console.WriteLine("外循环newname " + newname);

                        if (newname != "")
                        {
                            continue;
                        }
                        foreach (string subKeyName in versionKey.GetSubKeyNames())
                        {
                            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                            newname = (string)subKey.GetValue("Version", "");
                            if (string.Compare(newname, oldname) > 0)
                            {
                                oldname = newname;
                            }
                            Console.WriteLine("内循环oldname " + oldname);
                            Console.WriteLine("内循环newname " + newname);

                        }
                    }
                }
            }
            return string.Compare(oldname, version) > 0 ? true : false;//获取到的最大值与版本对比大小  
        }
        private static string GetDotNetVersion_big()
        {
            int i = 0;
            string oldname = "0";
            using (RegistryKey ndpKey =
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                {
                    Console.WriteLine("循环次数" + ++i);
                    if (versionKeyName.StartsWith("v"))
                    {
                        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                        string newname = (string)versionKey.GetValue("Version", "");
                        if (string.Compare(newname, oldname) > 0)
                        {
                            oldname = newname;

                        }
                        Console.WriteLine("外循环oldname " + oldname);
                        Console.WriteLine("外循环newname " + newname);

                        if (newname != "")
                        {
                            continue;
                        }
                        foreach (string subKeyName in versionKey.GetSubKeyNames())
                        {
                            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                            newname = (string)subKey.GetValue("Version", "");
                            if (string.Compare(newname, oldname) > 0)
                            {
                                oldname = newname;
                            }
                            Console.WriteLine("内循环oldname " + oldname);
                            Console.WriteLine("内循环newname " + newname);

                        }
                    }
                }
            }
            return oldname;
        } //仅仅获取到dotnet的最大值。



        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        } //打印

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            ////打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            ////打印内容 为 局部的 this.groupBox1
            //Bitmap _NewBitmap = new Bitmap(groupBox1.Width, groupBox1.Height);
            //groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            //e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 
            ////////Bitmap _NewBitmap = new Bitmap(295, 412);
            ////////e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 



            //////////打印内容 为 自定义文本内容 
            ////////Font font = new Font("宋体", 12);
            ////////Brush bru = Brushes.Blue;
            ////////for (int i = 1; i <= 5; i++)
            ////////{
            ////////    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            ////////}


        } //打印内容的设置示例。

        private void button1_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
            printPreviewControl1.InvalidatePreview();//立即刷新预览

        }//打开 打印设置

        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewControl1.InvalidatePreview();//立即刷新预览
            this.printPreviewDialog1.ShowDialog();

        }//预览

        private void button3_Click(object sender, EventArgs e)
        {

            //放这个位置程序会假死printPreviewControl1.InvalidatePreview();//立即刷新预览
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
            printPreviewControl1.InvalidatePreview();//立即刷新预览

        }//打开 打印对话框

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Size Bs = new Size(295, 412);
            //Bitmap _NewBitmap = new Bitmap(295, 412); //300dpi下1寸 1像素为单位
            //Bitmap _NewBitmap = new Bitmap(71,99);//72dpi下一寸
            // Bitmap _NewBitmap = new Bitmap(pictureBox1.Image,94, 132);//96dpi下一寸   
            //Bitmap _NewBitmap = new Bitmap(pictureBox1.Image, 104, 145);
            Bitmap _NewBitmap = new Bitmap(pictureBox1.Image, Bs);
            _NewBitmap.SetResolution(300.0f, 300.0f);

            // pictureBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            //Bitmap _NewBitmap_Plus = new Bitmap(pictureBox1.Image, 300, 300);//多张合成用    

            //pictureBox1.DrawToBitmap(_NewBitmap_Plus, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // e.Graphics.DrawImage(_NewBitmap_Plus, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 0, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 1, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 2, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 0, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 1, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 2, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 3, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 3, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 2, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 2, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 2, 104.0f, 145.0f);

            Font drawFont = new Font("黑体", 12);
            Font drawFont1 = new Font("黑体", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            //Image memoryImage1 = Image.FromFile(Application.StartupPath + @"\top.jpg"); //测试头像文件路径
            //e.Graphics.DrawImage(memoryImage1, 583, 200, 170, 240);
            //基本信息
            e.Graphics.DrawString(DateTime.Now.ToString(), drawFont, drawBrush, 0, (104.0f + 10.0f) * 4);
            string xdpi = e.Graphics.DpiX.ToString();
            string ydpi = e.Graphics.DpiY.ToString();
            e.Graphics.DrawString("X分辨率  " + xdpi, drawFont, drawBrush, 0, (104.0f + 10.0f) * 4 + 15);
            e.Graphics.DrawString("Y分辨率  " + ydpi, drawFont, drawBrush, 0, (104.0f + 10.0f) * 4 + 30);
            e.Graphics.DrawString("版本V" + this.ProductVersion + "运营管理中心制作", drawFont, drawBrush, 0, (104.0f + 10.0f) * 4 + 45);

            ////////打印内容 为 自定义文本内容 
            //////Font font = new Font("宋体", 12);
            //////Brush bru = Brushes.Blue;
            //////for (int i = 1; i <= 5; i++)
            //////{
            //////    e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            //////}
            try
            {
                
            }
            catch (Exception)
            {

                throw;
            }
        } //打印内容的设置

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            Size size = new Size(295, 412);
            //Bitmap _NewBitmap = new Bitmap(295, 412); //300dpi下1寸 1像素为单位
            //Bitmap _NewBitmap = new Bitmap(71,99);//72dpi下一寸
            // Bitmap _NewBitmap = new Bitmap(pictureBox1.Image,94, 132);//96dpi下一寸   
            //Bitmap _NewBitmap = new Bitmap(pictureBox1.Image, 104, 145);
            Bitmap _NewBitmap = new Bitmap(pictureBox1.Image, size);
            _NewBitmap.SetResolution(300.0f, 300.0f);

            // pictureBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // groupBox1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            //Bitmap _NewBitmap_Plus = new Bitmap(pictureBox1.Image, 300, 300);//多张合成用    


            //pictureBox1.DrawToBitmap(_NewBitmap_Plus, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            // e.Graphics.DrawImage(_NewBitmap_Plus, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 0, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 1, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 2, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 0, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 1, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 2, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 3, (_NewBitmap.Height + 10) * 0, _NewBitmap.Width, _NewBitmap.Height);
            ////e.Graphics.DrawImage(_NewBitmap, (_NewBitmap.Width + 10) * 3, (_NewBitmap.Height + 10) * 1, _NewBitmap.Width, _NewBitmap.Height);

            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 0, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 1, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 0, (145.0f + 10.0f) * 2, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 1, (145.0f + 10.0f) * 2, 104.0f, 145.0f);
            e.Graphics.DrawImage(_NewBitmap, (104.0f + 10.0f) * 2, (145.0f + 10.0f) * 2, 104.0f, 145.0f);

            Font drawFont = new Font("黑体", 20);
            Font drawFont1 = new Font("黑体", 16);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            //Image memoryImage = Image.FromFile(Application.StartupPath + @"\back.jpg"); //背景格式文件路径
            //e.Graphics.DrawImage(memoryImage, 0, 0, 825, 1178);

            //Image memoryImage1 = Image.FromFile(Application.StartupPath + @"\top.jpg"); //测试头像文件路径
            //e.Graphics.DrawImage(memoryImage1, 583, 200, 170, 240);
            //基本信息
            e.Graphics.DrawString(DateTime.Now.ToString(), drawFont, drawBrush, 0, (104.0f + 10.0f) * 4);
            string xdpi = e.Graphics.DpiX.ToString();
            string ydpi = e.Graphics.DpiY.ToString();
            e.Graphics.DrawString("X分辨率  " + xdpi, drawFont1, drawBrush, 0, (104.0f + 10.0f) * 4 + 25);
            e.Graphics.DrawString("Y分辨率  " + ydpi, drawFont1, drawBrush, 0, (104.0f + 10.0f) * 4 + 40);




        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            pictureBox1.Load(openFileDialog1.FileName);
            
            printPreviewControl1.InvalidatePreview();//立即刷新预览

        }//确认文件对话框

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
               e.Effect = DragDropEffects.Link;
            else
            
                e.Effect = DragDropEffects.None;

            
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            System.Array datas = (System.Array)e.Data.GetData(DataFormats.FileDrop);
             string filePathOrDirectory = (datas).GetValue(0).ToString();
            if (System.IO.Directory.Exists(filePathOrDirectory))         
                MessageBox.Show("是目录：" + filePathOrDirectory);          
            else
            {

                string ispng = @"(.*)(\.png)$";
                string isbmp = @"(.*)(\.bmp)$";
                string isjpeg = @"(.*)(\.jpeg)$";
                string isjpg = @"(.*)(\.jpg)$";
                if (System.Text.RegularExpressions.Regex.IsMatch(filePathOrDirectory, isjpg)|System.Text.RegularExpressions.Regex.IsMatch(filePathOrDirectory, ispng)| System.Text.RegularExpressions.Regex.IsMatch(filePathOrDirectory, isbmp)| System.Text.RegularExpressions.Regex.IsMatch(filePathOrDirectory, isjpeg))
                {
                    pictureBox1.Load(filePathOrDirectory);
                    printPreviewControl1.InvalidatePreview();//立即刷新预览

                }
                else
                    MessageBox.Show("未知图片类型：" + filePathOrDirectory);




            }

        }//拖入图片类型判断

        private void btn_easyprint_Click(object sender, EventArgs e)
        {   
            //this.pd.Print();
          
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
            printPreviewControl1.InvalidatePreview();//立即刷新预览
         
        }

       
        private void lbl_i_MouseClick(object sender, MouseEventArgs e)
        {
            lbl_i.Text = i_text;
         
        }

        private void printPreviewControl1_Click(object sender, EventArgs e)
        {

        }

        private void lbl_i_Click(object sender, EventArgs e)
        {

        }
    }
}
